<?php
/********************************************************
 *
 * 此文件的作用:
 *		包括勤助正式员工的所有操作
 *
 * @copyright Copyright (c) 2008-2009
 * @author 刘昌功 lcg635@126.com
 *
 ********************************************************/
defined('APP_DIR') or header("HTTP/1.0 404 Not Found");

FLEA::loadClass('Common_Model');

class Model_Teacher extends Common_Model {
	function __construct() {
		parent::__construct();
		$this->_tb = FLEA::getSingleton('Table_Teacher');
	}
	
	function getTeachersByType($type = 1, $page = 0) {
		$_modUser = FLEA::getSingleton('Model_User');
		$pageSize = 20;
		//$links = array('user');
		$teachers = $this->_getList('type = '.$type, '*', null, $pageSize, $links, true, $page);
		foreach ($teachers['lists'] as $k => $v) {
			$teachers['lists'][$k]['user'] = $_modUser->getUserInfo($v['user_id']);
		}
		//$this->_getAllData($teachers);
		return $teachers;
	}
	
	function search1($data = null, $page = 0) {
		$_modUser = FLEA::getSingleton('Model_User');
		$pageSize = 20;
		//$links = array('user');
		$advantage='语文';
		$teachers = $this->_getList('advantage like '.$advantage, '*', null, $pageSize, $links, true, $page);
		foreach ($teachers['lists'] as $k => $v) {
			$teachers['lists'][$k]['user'] = $_modUser->getUserInfo($v['user_id']);
		}
		//$this->_getAllData($teachers);
		return $teachers;
	}
	
	function search($data = null, $page = 0){
		$_modUser = FLEA::getSingleton('Model_User');
		$pageSize = 10;
		$data2 = $data;
		unset($data2['type']);
		if (empty($data2) && !empty($_COOKIE['teacherSQL'])) $query = $_COOKIE['teacherSQL'];
		else {
			$query = "select *  from user, teacher where user.user_id = teacher.user_id ";
			//按教员姓名查找
			if($data['trueName']) $query .= " and user.truename like '%" . $data['trueName'] . "%'";
			
			//按教员编号查找
			if($data['teacher_id']) $query .= ' and user.user_id = ' . $data['teacher_id'] ;
			
			//按专业查找
			if($data['major']) $query .= " and user.major like '%" . $data['major'] . "%'";
			
			//按专长学科查找
			//if($data['advantage']) $query .= ' and teacher.advantage like \'%' . $data['advantage'] . '%\'' ;
			
			if($data['advantage']) {
				$advantage1 = split(';', $data['advantage']);	//特长学科的搜索 08-11-10
				$count1 = count($advantage1);
				do {
					$query .= ' and teacher.advantage like \'%' . $advantage1[--$count1] . '%\'' ;
					//$query .= ' and patindex(teacher.advantage,'%$advantage1[--$count]%')>0' ;
				}while($count1 >= 0);
			}
			
			//按学号模糊查找
			if($data['stuNum']) $query .= ' and user.num like \'%' . $data['stuNum'] .'%\'';
			
			if(isset($data['type'])) $query .= ' and teacher.type = ' . $data['type'];

			setcookie('teacherSQL', $query);
			$_SESSION['sForm'] = serialize($data);
		}

		$result = $this->_tb->execute($query);
		
		while ($row = mysql_fetch_assoc($result)) {
			$teachers['lists'][] = $row;
		}

		foreach ($teachers['lists'] as $k => $v) {
			$teachers['lists'][$k]['user'] = $_modUser->getUserInfo($v['user_id']);
		}
		
		$totalCount = count($teachers['lists']);
		$lastPage = ceil($totalCount/$pageSize) - 1;
		$prevPageNumber = $page < 1 ? 0 : $page - 1;
		$nextPageNumber = $page >= $lastPage ? $lastPage : $page + 1;
		$mimi['pagerData'] = array(
			'totalCount' => $totalCount,
			'pageCount' => ceil($totalCount/$pageSize),
			'firstPage' => 0,
			'currentPage' => $page,
			'lastPage' => $lastPage,
			'prevPage' => $prevPageNumber,
			'nextPage' => $nextPageNumber,
		);
		
		$last = ($page+1) *($pageSize) >= $totalCount ? $totalCount : ($page+1) *($pageSize);
			
		for($i = $page * $pageSize ; $i < $last ; $i++ )
			$mimi['lists'][]  = $teachers['lists'][$i];

		return $mimi;

	}
		
	function _getList($cond, $fields, $sortby = 'created desc', $limit = 10, $links = null, $pager = false, $page = 0) {
		!is_null($links) && $this->_tb->enableLinks($links);
		if (true == $pager) {
			FLEA::loadHelper('pager');
			$pager = new FLEA_Helper_Pager($this->_tb, $page, $limit, $cond, $sortby);
			$result = array(
				'lists' => $pager->findAll($fields),
				'pagerData' => $pager->getPagerData()
			);
		} else {
			$result['lists'] = $this->_tb->findAll($cond, $sortby, $limit, $fields);
		}
		return $result;
	}
	
	function _getAllData(& $rows) {
		foreach ($rows['lists'] as $row) {
			
		}
	}
	
	function getInfo($user_id) {
		return $this->_tb->find('user_id=' . $user_id);
	}
	

	function hasRegistered($uid) {
		return $this->_tb->findCount('user_id = ' . $uid);
	}

	function register($data, $user_id) {
		if ( !is_array($data) ) {
			$result[] = '提交的数据为空！';
			return $result;
		}
		$stuType = '';
		foreach ( $data['stuType'] as $v )
			$stuType .= ',' . $v;
		$data['stuType'] = substr($stuType, 1);
		$data['user_id'] = $user_id;
		empty($data['score']) && $data['score'] = 0;
		empty($data['mandarin']) && $data['mandarin'] = 0;
		empty($data['cantonese']) && $data['cantonese'] = 0;
		empty($data['experience']) && $data['experience'] = 0;
		if( $this->_tb->findCount('user_id = ' . $user_id) > 0 )
			$result = $this->_tb->update($data);
		else
			$result = $this->_tb->create($data);
		return $result;
	}
	
	function unVerifyTeacher($user_id) {
		return $this->_tb->updateField('user_id='.$user_id, 'type', 2);
	}
	
	function verifyTeacher($user_id) {
		return $this->_tb->updateField('user_id='.$user_id, 'type', 1);
	}
	
	function blackListTeacher($user_id) {
		return $this->_tb->updateField('user_id='.$user_id, 'type', 3);
	}
	
	function delete($user_id) {
		return $this->_tb->removeByConditions('user_id='.$user_id);
	}
	
	function restoreTeacher($user_id) {	//还原审核失败教员为待审教员	10-30 永华
		return $this->_tb->updateField('user_id='.$user_id, 'type', 0);
	}
	
	function getForm($type, $data = null) {
		$form = $this->_tb->form($type, $data);
		return $form;
	}
	
	function postMessage($data, $user_id) {
		
		
	}
}
